"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Hall_1 = __importDefault(require("./Hall"));
const __1 = require("..");
const utils_1 = require("../shared/common/utils");
class Room {
    constructor() {
        // 玩家
        this.rooms = new Map();
        // 最大房间数
        this.capacity = 500;
    }
    static getInstance() {
        if (this._instance === null) {
            this._instance = new this();
        }
        return this._instance;
    }
    static get instance() {
        return this.getInstance();
    }
    create(player, password) {
        // 生产唯一id
        const num_pre = String((0, utils_1.random)(10, 99));
        const num_inc = String(this.rooms.size);
        const id = num_pre + num_inc;
        const playerBattle = player;
        playerBattle.status = 0;
        const room = {
            id,
            password,
            players: [playerBattle],
            status: 0
        };
        this.rooms.set(id, room);
        // refresh
        this.refreshHall();
        return room;
    }
    join(id, player) {
        const room = this.get(id);
        if (room) {
            const playerBattle = player;
            playerBattle.status = 0;
            room.players.push(playerBattle);
            // refresh
            this.refreshRoom(room.id);
            this.refreshHall();
        }
        return room;
    }
    remove(id) {
        this.rooms.delete(id);
    }
    get(id) {
        let room = undefined;
        if (this.rooms.has(id)) {
            room = this.rooms.get(id);
        }
        return room;
    }
    lists() {
        return Array.from(this.rooms.values());
    }
    // 能否创建房间：判断房间数量
    canCreate() {
        return this.rooms.size <= this.capacity;
    }
    // 能否加入房间：判断房间是否满员
    canJoin(id) {
        const room = this.get(id);
        if (room) {
            // 游戏中的房间
            if (room.status != 0)
                return false;
            return room.players.length < 4;
        }
        return false;
    }
    removeByOpenid(openid) {
        const lists = this.lists();
        let room = null;
        for (let i = 0; i < lists.length; i++) {
            const players = lists[i].players;
            for (let j = 0; j < players.length; j++) {
                if (players[j].openid == openid) {
                    room = lists[i];
                    break;
                }
                if (room)
                    break;
            }
        }
        if (room) {
            const players = room.players;
            if (players.length) {
                const removeIndex = players.findIndex(player => player.openid == openid);
                players.splice(removeIndex, 1);
                if (players.length) {
                    // refresh
                    this.refreshHall();
                    this.refreshRoom(room.id);
                    this.refreshGame(room.id);
                }
                else {
                    // 移除空房间 
                    this.remove(room.id);
                    // refresh
                    this.refreshHall();
                }
            }
        }
    }
    // 大厅中, 通知路人
    refreshHall() {
        const lists = this.lists();
        // 筛选人数不满的房间
        const rooms = lists.filter(room => room.players.length < 4 && room.players.length > 0 && room.status == 0);
        // 广播对象
        const connArr = Hall_1.default.instance.lists();
        // 广播内容
        const msg = {
            type: 'msg_hall_refresh',
            content: {
                rooms: rooms
            }
        };
        __1.server.broadcastMsg('GameRefresh', msg, connArr);
    }
    // 房间中, 通知房间成员
    refreshRoom(roomId) {
        const room = this.get(roomId);
        if (room) {
            const connArr = [];
            room.players.forEach(player => {
                connArr.push(Hall_1.default.instance.get(player.openid));
            });
            // 广播对象
            const msg = {
                type: 'msg_room_refresh',
                content: {
                    room
                }
            };
            __1.server.broadcastMsg('GameRefresh', msg, connArr);
        }
    }
    // 游戏中, 断线通知
    refreshGame(roomId) {
        const room = this.get(roomId);
        if (room) {
            // 广播对象
            const connArr = [];
            room.players.forEach(player => {
                connArr.push(Hall_1.default.instance.get(player.openid));
            });
            // 广播内容
            const msg = {
                type: 'msg_game_disconnect',
                content: {
                    room
                }
            };
            __1.server.broadcastMsg('GameRefresh', msg, connArr);
        }
    }
}
exports.default = Room;
Room._instance = null;
